(function (p) {
    
    var LessonSchedule = function ( container, date, hour, monutes, validationScript ) {
        
        this.container          = container
        this.date               = date;
        this.hour               = hour
        this.minutes            = monutes;
        this.validationScript   = validationScript
        
        this.date.datepicker( { numberOfMonths: 2, minDate: 0 } );
        
        this.date.change($.proxy(this.onChange, this));
        this.hour.change($.proxy(this.onChange, this));
        this.minutes.change($.proxy(this.onChange, this));
        
    }
    LessonSchedule.fn = LessonSchedule.prototype;
    
    
    LessonSchedule.fn.validate = function ( validationScript ) {
        
        //console.log("validate");
        this.changeState(LessonSchedule.LOADING);
        $.ajax({
            type: "POST",
            url: validationScript,
            data: {
                date: this.date.val(),
                hour: this.hour.val(),
                minute: this.minutes.val()
            },
            success: $.proxy(this.onValidationReceived, this),
            error: $.proxy(this.onValidationError, this)
        });
        
    }
      
    
    LessonSchedule.fn.changeState = function ( state ) {
        
        var label;
        
        switch (state) {
            case LessonSchedule.LOADING:
                label = $(LessonSchedule.LOADING_LABEL);
            break
            case LessonSchedule.AVAILABLE:
                label = $(LessonSchedule.AVAILABLE_LABEL);
            break
            case LessonSchedule.NO_AVAILABLE:
                label = $(LessonSchedule.NO_AVAILABLE_LABEL);
            break
            case LessonSchedule.INCORRECT_DATE:
                label = $(LessonSchedule.INCORRECT_DATE_LABEL);
            break
        }
        this.container.find("span.label").remove();
        
        if(label){
            this.container.append(label);
        }
        
        this.status = state;
        
    }
    
    
    LessonSchedule.fn.onChange = function ( event ) {
        
        var date = new Date(this.date.val());
        //console.log(Boolean(date.getTime()));
        if(this.date.val() && 
           this.hour.val() && 
           this.minutes.val() ) {
            
            if(!date.getTime()){
            
                this.changeState(LessonSchedule.INCORRECT_DATE);
                return
                
            }
            
            this.validate(this.validationScript);
            
        }
        
    }
    
    
    LessonSchedule.fn.onValidationReceived = function ( data ) {
        
        //console.log("onValidationReceived");
        //console.log(data);
        
        if(Number(data)){
            
            this.changeState(LessonSchedule.AVAILABLE);
            
        }else{
            
            this.changeState(LessonSchedule.NO_AVAILABLE);
            
        }
        
    }
    
    
    LessonSchedule.fn.onValidationError = function ( jqXHR, textStatus ) {
        
        console.log(jqXHR);
        alert(textStatus + " - Mirar información detallada del error en la consola para desarroladores del explorador");
        
    }
    
    
    LessonSchedule.LOADING_LABEL        = '<span class="label"><span class="icon back-in-time"></span> Comprobando disponibilidad</span>';
    LessonSchedule.AVAILABLE_LABEL      = '<span class="label label-success"><span class="icon check"></span> Disponible</span>';
    LessonSchedule.NO_AVAILABLE_LABEL   = '<span class="label label-danger"><span class="icon cross"></span> No disponible</span>';
    LessonSchedule.INCORRECT_DATE_LABEL = '<span class="label label-danger"><span class="icon cross"></span> Fecha incorrecta</span>';
    
    LessonSchedule.LOADING        = "loading";
    LessonSchedule.AVAILABLE      = "available";
    LessonSchedule.NO_AVAILABLE   = "no-available";
    LessonSchedule.INCORRECT_DATE = "incorrect-date";
            
    
    p.LessonSchedule = LessonSchedule;
    
})(Sai);



$(document).ready(function(){
    
        this.lessonSchedule = new Sai.LessonSchedule($("label.lessson-specific-schedule"),
                                                     $("label.lessson-specific-schedule > input[name=date]"),
                                                     $("label.lessson-specific-schedule > select[name=hour]"),
                                                     $("label.lessson-specific-schedule > select[name=minutes]"),
                                                     Sai.HOST + "modules/" + Sai.MODULE + "/actions/create/scripts/validate-date.php");
    
});